package com.hyc.myvideo.ui.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.UserInfo;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.ui.view.DialogPhoto;
import com.hyc.myvideo.utils.imguitl.ImgUitl;
import com.hyc.utils.StringUtils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * 编辑资料
 * @author gyb
 * @version 1.0
 * @created 2017/07/12
 */
public class EditActivity extends BaseFActivity{
    /*头像*/
    @BindView(R.id.img_edit_head_id)
    ImageView imageViewHead;
    /*昵称*/
    @BindView(R.id.t_nickname)
    TextView textViewNicknam;
    /*手机号*/
    @BindView(R.id.t_phone)
    TextView textViewPhone;
    /**照片选择弹框*/
    private DialogPhoto dialogPhoto = null;

    @Override
    public int getContentViewId() {
        return R.layout.activity_edit;
    }

    @Override
    protected void initView() {
        setLeftBtnClick(true,getStr(R.string.huc_my_edit));
        showNickname(userInfo.getUser_nickname());
        showPhone(userInfo.getUser_phone());
        showPortrait(userInfo.getUser_face());
        dialogPhoto = new DialogPhoto(photoInterface);
    }

    /***
     * 显示昵称
     * @param nickname
     */
    private void showNickname(String nickname){
        if(StringUtils.isEmpty(nickname)){
            return;
        }
        textViewNicknam.setText(nickname);
    }

    /***
     * 显示手机号
     * @param phone
     */
    private void showPhone(String phone){
        if(StringUtils.isEmpty(phone)){
            return;
        }
        textViewPhone.setText(phone);
    }

    /***
     * 显示头像
     * @param imgurl
     */
    private void showPortrait(String imgurl){
        ImgUitl.setLocalRoundImg(imgurl,imageViewHead,R.dimen.app_img_hw70);
    }



    @Override
    protected void initData() {

    }

    @Override
    protected void oNext(String r, String method) {
        BaseResult<UserInfo> result = JSONObject.parseObject(r, new TypeReference<BaseResult<UserInfo>>() {});
        if(result.isCode()){
            UserInfo userInfo = result.getResult();
            String imgUrl = userInfo.getUser_face();
            appContext.getUserInfo().setUser_face(imgUrl);
            showPortrait(imgUrl);
        }
        Toaster.show(result.getMsg());
    }

    @Override
    protected void oError() {

    }

    /***
     * 照片选择
     */
    DialogPhoto.photoInterface photoInterface = new DialogPhoto.photoInterface() {
        @Override
        public void onPictures() {
            // 是否是直接打开相机
            startActivityForResult(new Intent(EditActivity.this, ImageGridActivity.class).putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS,true), 0);
        }
        @Override
        public void onAlbum() {
            startActivityForResult(new Intent(EditActivity.this, ImageGridActivity.class), 0);
        }
    };



    @OnClick({R.id.r_img_id,R.id.r_nickname_id,R.id.r_phone_number_id})
    public void onClicks(View view) {
        switch (view.getId()){
            /*编辑头像*/
            case R.id.r_img_id:
                dialogPhoto.show(this.getSupportFragmentManager(), "照片选择");
                break;
            /*编辑昵称*/
            case R.id.r_nickname_id:
                startActivityForResult(new Intent(this, NicknameActivity.class).putExtra("nickname", getText(textViewNicknam)), Constants.RESULT_CODE);
                break;
            /*编辑手机号*/
            case R.id.r_phone_number_id:
                startActivityForResult(new Intent(this, EditPhoneActivity.class).putExtra("phone", getText(textViewPhone)), Constants.RESULT_CODE_TWO);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            switch (resultCode){
                //头像
                case ImagePicker.RESULT_CODE_ITEMS:
                    if (data != null && requestCode == 0) {
                        ArrayList<ImageItem> imagePath = (ArrayList<ImageItem>)data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                        if (imagePath != null || imagePath.isEmpty()){
                            File mFile = new File(imagePath.get(0).path);
                            api.uploadFiles(mFile);
                        }
                    } else {
                        Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case Constants.RESULT_CODE:
                    Bundle bundle = data.getExtras();
                    String nickname = bundle.getString("nickname");
                    showNickname(nickname);
                    break;
                case Constants.RESULT_CODE_TWO:
                    Bundle bundlePhone = data.getExtras();
                    String phone = bundlePhone.getString("phone");
                    showPhone(phone);
                    break;
            }
        }catch (Exception e){
            Timber.e(e,"用户资料编辑回调异常");
        }
    }
}
